/* 封装axios模块 */
import axios from 'axios'
import JsonBig from 'json-bigint'

// 创建axios实例，配置基础请求路径
const requst = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn',
  transformResponse: [function (data) {
    try {
      return JsonBig.parse(data)
    } catch (e) {
      return data
    }
  }]
})

// 配置请求响应头
requst.interceptors.request.use((config) => {
  const user = JSON.parse(window.localStorage.getItem('user'))
  if (user) config.headers.Authorization = `Bearer ${user.token}`
  return config
}, error => {
  return Promise.reject(error)
})

// 导出
export default requst
